package com.andnnl.threadtest;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.*;

/**
 * Created by chenss on 2020/5/20.
 */
public class DataStore {
    public static ThreadPoolExecutor threadPoolExecutor=new ThreadPoolExecutor(7,7,
            1, TimeUnit.MINUTES,new ArrayBlockingQueue<Runnable>(100));
    public static List<Integer> data= new ArrayList<>();//Collections.synchronizedList(new ArrayList<Integer>());
    final static int dataSize=20000000;
    public static void initData() throws ExecutionException, InterruptedException {
        List<CompletableFuture<List<Integer>>> tasks=new ArrayList<>();
        LinkedBlockingQueue<List<Integer>> queue=new LinkedBlockingQueue<>();
        CountDownLatch counter=new CountDownLatch(10);
        CompletableFuture<Boolean> endTask=CompletableFuture.supplyAsync(()->{
            while (counter.getCount()>0 ||
                    !queue.isEmpty()){
                try {
                    data.addAll(queue.take());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            return true;
        });
        for (int i=0;i<10;i++){
            final int idx=i;
            CompletableFuture<List<Integer>> task=CompletableFuture.supplyAsync(()->{
                System.out.println("Task "+String.valueOf(idx)+" is running");
                List<Integer> lvRet=new ArrayList<>();
                long t1=System.currentTimeMillis();
                Random random=new Random();
                for (int j=0;j<dataSize/10;j++){
                    lvRet.add(random.nextInt());
                }
                long t2=System.currentTimeMillis();
                System.out.println("Task "+String.valueOf(idx)+" done. times:"+(t2-t1));
                queue.offer(lvRet);
                counter.countDown();
                return lvRet;
            },threadPoolExecutor);
            tasks.add(task);
           /* task.whenComplete((d,e)->{
                data.addAll(d);
                counter.countDown();
            });*/
        }
        counter.await();
        endTask.get();
        System.out.println("done,size:"+data.size());
    }
    private static void initData2(){
        for (int i=0;i<10;i++) {
            Random random = new Random();
            for (int j = 0; j < dataSize/10; j++) {
                data.add(random.nextInt());
            }
        }
        System.out.println("done,size:"+data.size());
    }
    public static  void main(String[] args){
        try {
            long t=System.currentTimeMillis();
            initData();
            System.out.println(System.currentTimeMillis()-t);
            data.clear();
            long tt=System.currentTimeMillis();
            initData2();
            System.out.println(System.currentTimeMillis()-tt);
            threadPoolExecutor.shutdownNow();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}